from app import db
from werkzeug.security import generate_password_hash,check_password_hash
from datetime import datetime


class BaseModel(db.Model):
    __abstract__ =True
    create_time = db.Column(db.DateTime,default=datetime.now)
    update_time = db.Column(db.DateTime,default=datetime.now,onupdate=datetime.now)

tb_user_collection = db.Table('user_collection',
            db.Column('user_id',db.Integer,db.ForeignKey('user_info.id'),primary_key=True),
            db.Column('book_id',db.Integer,db.ForeignKey('book.id'),primary_key=True))


class User(BaseModel):
    __tablename__='user_info'
    id = db.Column(db.Integer,primary_key=True)
    nick_name = db.Column(db.String(30),unique=True,nullable=False)
    password = db.Column(db.String(128),nullable=False)
    email = db.Column(db.String(56),nullable=False,unique=True)
    mobile = db.Column(db.String(11),nullable=False,unique=True)
    balance =db.Column(db.Integer,default=0)
    gender = db.Column(db.Enum('MAN','WOMAN'),default='MAN')
    is_admin = db.Column(db.Boolean)
    cart_id = db.Column(db.Integer,db.ForeignKey('cart.id'))
    cart = db.relationship('Cart',uselist=False)
    news = db.relationship('News',backref='admin',lazy='dynamic')
    collection_books = db.relationship('Book',secondary=tb_user_collection,lazy='dynamic')


class Book(BaseModel):
    __tablename__='book'
    id = db.Column(db.Integer,primary_key=True)
    title = db.Column(db.String(50),nullable=False)
    author = db.Column(db.String(30),default='未名')
    image_url = db.Column(db.String(326))
    summary = db.Column(db.String(320))
    price = db.Column(db.Integer)
    cost = db.Column(db.Integer)
    publisher = db.Column(db.String(20))
    status = db.Column(db.Integer)
    category_id = db.Column(db.Integer,db.ForeignKey('info_category.id'))
    carts = db.relationship('Cart',backref='book',lazy='dynamic',)
    items = db.relationship('OrderItems',backref='bookinfo',lazy='dynamic')



class Category(BaseModel):
    __tablename__ = 'info_category'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(20),nullable=False)
    books = db.relationship('Book',backref='cates',lazy='dynamic')



class Cart(BaseModel):
    __tablename__ = 'cart'
    id = db.Column(db.Integer,primary_key=True)
    book_id = db.Column(db.Integer,db.ForeignKey('book.id'))
    user_id = db.Column(db.Integer,db.ForeignKey('user_info.id'))
    count = db.Column(db.Integer,default=1)
    buyer = db.relationship('User')



class Order(BaseModel):
    __tablename__='order'
    id = db.Column(db.Integer,primary_key=True)
    user_id = db.Column(db.Integer,db.ForeignKey('user_info.id'))
    paytime = db.Column(db.DateTime)
    receiver = db.Column(db.String(50),nullable=False)
    address = db.Column(db.String(200),nullable=False)
    status = db.Column(db.SmallInteger,default=0)
    items = db.relationship('OrderItems',backref='orders',lazy='dynamic')


class OrderItems(BaseModel):
    __tablename__ = 'order_items'
    id = db.Column(db.Integer,primary_key=True)
    book_id = db.Column(db.Integer,db.ForeignKey('book.id'))
    order_id = db.Column(db.Integer,db.ForeignKey('order.id'))
    count = db.Column(db.Integer,default=1)


class News(BaseModel):
    __tablename__ = 'news'
    id = db.Column(db.Integer,primary_key=True)
    title = db.Column(db.String(30),nullable=False)
    content = db.Column(db.Text,nullable=False)
    admin_id = db.Column(db.Integer,db.ForeignKey('user_info.id'))

